package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.bo.admin.category.MarketCategoryUpdateBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryCreateVo;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryL1Vo;
import com.cskaoyan.bean.vo.admin.category.MarketCategoryListVo;
import com.cskaoyan.service.admin.MarketBrandService;
import com.cskaoyan.service.admin.MarketCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;

/**
 * 模块:商场管理/商品类目
 * url:/admin/category
 *
 * @Author: zhang
 * @Date: 2022/11/20 14:32
 * @Since: jdk1.8
 */
@RestController
@RequestMapping("admin/category")
public class MarketCategoryController {
    @Autowired
    MarketBrandService marketBrandService;
    @Autowired
    MarketCategoryService marketCategoryService;

    /**
     * 查询所有的商品类目信息
     * /admin/category/list
     *
     * @return BaseRespVo<CommonData < MarketCategoryListVo>>
     */
    @RequestMapping("list")
    public BaseRespVo<CommonData<MarketCategoryListVo>> list() {
        CommonData<MarketCategoryListVo> data = marketCategoryService.list();
        return BaseRespVo.ok(data);
    }

    /**
     * 显示所有1级类目
     * /admin/category/l1
     *
     * @return BaseRespVo<MarketBrand>
     */
    @RequestMapping("l1")
    public BaseRespVo<CommonData<MarketCategoryL1Vo>> l1() {
        CommonData<MarketCategoryL1Vo> data = marketCategoryService.l1();
        return BaseRespVo.ok(data);
    }

    /**
     * 创建一个品类并且返回创建信息
     * /admin/category/create
     *
     * @param marketCategory 接收到的创建信息
     * @return BaseRespVo<MarketBrand>
     * @throws SQLException 创建失败
     */
    @PostMapping("create")
    public BaseRespVo<MarketCategoryCreateVo> create(@RequestBody MarketCategory marketCategory) throws SQLException {
        MarketCategoryCreateVo data = marketCategoryService.create(marketCategory);
        if(data==null){
            return BaseRespVo.error("该商品种类已经存在");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * 修改品类信息
     * /admin/category/update
     *
     * @param marketCategoryUpdateBo 要修改的种类信息
     * @return 返回修改后的品牌信息
     */
    @RequestMapping("update")
    public BaseRespVo<Object> update(@RequestBody MarketCategoryUpdateBo marketCategoryUpdateBo) {
        marketCategoryService.update(marketCategoryUpdateBo);
        return BaseRespVo.ok();
    }

    /**
     * 删除品类
     * /admin/category/delete
     *
     * @param marketCategoryUpdateBo 要删除的品类信息
     * @return ok
     */
    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody MarketCategoryUpdateBo marketCategoryUpdateBo) {
        marketCategoryService.delete(marketCategoryUpdateBo);
        return BaseRespVo.ok();
    }
}
